package com.xjrsoft.module.customerTwo.EMP.price_setting.service.impl;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjrsoft.module.customerTwo.EMP.price_setting.entity.TPriceSetting;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.xjrsoft.core.tool.utils.CollectionUtil;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import com.xjrsoft.module.customerTwo.EMP.price_setting.dto.TPriceSettingListDto;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import java.util.List;
import com.xjrsoft.module.customerTwo.EMP.price_setting.mapper.TPriceSettingMapper;
import com.xjrsoft.module.customerTwo.EMP.price_setting.service.ITPriceSettingService;
import com.xjrsoft.core.mp.base.BaseService;
import com.xjrsoft.core.tool.utils.Func;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

/**
 *  服务实现类
 *
 * @author dongxinhang
 * @since 2023-07-20
 */
@Service
@AllArgsConstructor
public class TPriceSettingServiceImpl extends BaseService<TPriceSettingMapper, TPriceSetting> implements ITPriceSettingService {


	@Override
	public IPage<TPriceSetting> getPageList(TPriceSettingListDto pageListDto) {
		Wrapper<TPriceSetting> wrapper = Wrappers.<TPriceSetting>query().lambda();
		return this.page(ConventPage.getPage(pageListDto), wrapper);
	}

	@Override
	public String addTPriceSetting(TPriceSetting tPriceSetting) {
		String tPriceSettingId = IdWorker.get32UUID();
		tPriceSetting.setId(tPriceSettingId);
		boolean isSuccess = this.save(tPriceSetting);
		return isSuccess ? tPriceSettingId : null;
	}

	@Override
	public boolean updateTPriceSetting(String id, TPriceSetting tPriceSetting) {
		tPriceSetting.setId(id);
		return this.updateById(tPriceSetting);
	}

	public boolean delete(String ids) {
		List<String> idList = Func.toStrList(ids);
		if (this.removeByIds(idList)) {
			return true;
		}
		return false;
	}

	@Override
	public BigDecimal getElectricityPrice() {
		Wrapper<TPriceSetting> wrapper = Wrappers.<TPriceSetting>query().lambda()
				.lt(true,TPriceSetting::getElectricityActiveTime, LocalDate.now());
		List<TPriceSetting> list = this.list(wrapper);
		if(CollectionUtil.isNotEmpty(list)){
			return list.get(0).getElectricityPrice();
		}
		return new BigDecimal("0");
	}

	@Override
	public BigDecimal getColdWaterPrice() {
		Wrapper<TPriceSetting> wrapper = Wrappers.<TPriceSetting>query().lambda()
				.lt(true,TPriceSetting::getColdWaterActiveTime, LocalDate.now());
		List<TPriceSetting> list = this.list(wrapper);
		if(CollectionUtil.isNotEmpty(list)){
			return list.get(0).getColdWaterPrice();
		}
		return new BigDecimal("0");
	}

	@Override
	public BigDecimal getHeatWaterPrice() {
		Wrapper<TPriceSetting> wrapper = Wrappers.<TPriceSetting>query().lambda()
				.lt(true,TPriceSetting::getHotWaterActiveTime, LocalDate.now());
		List<TPriceSetting> list = this.list(wrapper);
		if(CollectionUtil.isNotEmpty(list)){
			return list.get(0).getHotWaterPrice();
		}
		return new BigDecimal("0");
	}

}